import React, { Component } from 'react';

export default class Search extends Component {
  state = {
    keyword: '',
  }

  onChange = (e) => {
    this.setState({ keyword: e.target.value });
  }

  onSearch = () => {
    const { keyword } = this.state;
    const { onSearch } = this.props;
    onSearch(keyword);
  }

  onKeyPress = (e) => {
    if (e.key === 'Enter') {
      this.onSearch();
    }
  }

  render() {
    return (
      <div className="input-group mb-3">
        <input
          type="text"
          className="form-control"
          placeholder="输入关键字"
          aria-label="Recipient's username"
          aria-describedby="basic-addon2"
          onChange={this.onChange}
          onKeyPress={this.onKeyPress}
        />
        <div className="input-group-append">
          <button
            className="btn btn-outline-secondary"
            type="button"
            onClick={this.onSearch}
          >
            搜索
          </button>
        </div>
      </div>
    )
  }
}
